package com.mmall.common;

import org.codehaus.jackson.annotate.JsonIgnore;

import java.io.Serializable;

public class ServerResponse<T> implements Serializable {

    private int state;
    private String msg;
    private T data;

    private ServerResponse(int state){
        this.state = state;
    }

    private ServerResponse(String msg){
        this.msg = msg;
    }

    private ServerResponse(T data){
        this.data = data;
    }

    private ServerResponse(int state,String msg){
        this.state = state;
        this.msg = msg;
    }

    private ServerResponse(int state,T data){
        this.state = state;
        this.data = data;
    }

    private ServerResponse(String msg,T data){
        this.msg = msg;
        this.data = data;
    }

    @JsonIgnore
    //使之不在json序列化中
    public boolean is_success() {
        if (this.state ==ResponseCode.SUCCESS.getCode()) {
            return true;
        }
        return false;

    }

    public int getState() {
        return state;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }

    public static <T> ServerResponse<T> createBySuccess(){
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode());
    }

    public static <T> ServerResponse<T> createBySuccessMessage(String msg){
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),msg);
    }

    public static <T> ServerResponse<T> createBySuccess(T data){
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),data);
    }

    public static <T> ServerResponse<T> createBySuccess(String msg,T data){
        return new ServerResponse<T>(msg,data);
    }

    public static <T> ServerResponse<T> createByError(){
        return new ServerResponse<T>(ResponseCode.ERROR.getCode());
    }

    public static <T> ServerResponse<T> createByErrorMessage(String msg){
        return new ServerResponse<T>(ResponseCode.ERROR.getCode(),msg);
    }

    public static <T> ServerResponse<T> createByError(int state,String msg){
        return new ServerResponse<T>(state,msg);
    }

}
